$(document).ready(function(e){
	createPageTab();
	createSellerListTab();
	readyAllCompany("companySelect","marketSelect");
	findSellerList(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var sellers = null;
var curPageNum = null;
function findSellerList(pageNow){
	var tmp2=root+"/admin/seller!findSellersByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "superMarket.id":$("#marketSelect").val(), "seller.name": $("#sellerName").val(),
				"seller.jobNumber": $("#sellerNumber").val(), "seller.post": $("#sellerPost").val()},
		dataType:"json",
		success: function(json){
			if(sellers!=null) createSellerListTab();
			totalPageNum = json.totalPageNum;
			sellers = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].jobNumber+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].post+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].superMarket.company.name+' - '+json.pageData[i].superMarket.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].yibiao+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].kaoqin+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].zhishi+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].jiqiao+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdateSellerDiv('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="deleteSeller('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findSellerList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findSellerList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findSellerList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findSellerList(json.totalPageNum);});
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findSellerList(p);
	}
}

function showAddSellerDiv(){
	$("#backGroud").show();
	$("#addDiv").show();
	if($("#companySelect_add").html()==""){
		readyAllCompany("companySelect_add","marketSelect_add");
		readyAllMarket("companySelect_add","marketSelect_add");
	}
}

function closeAddSellerDiv(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function saveSeller(){
	if($("#sellerName_add").val()=="" || $("#sellerName_add").val()==null){
		alert("促销员姓名不能为空");
		return;
	}
	if($("#marketSelect_add").val()=="" || $("#marketSelect_add").val()==null){
		alert("店名不能为空");
		return;
	}
	if($("#sellerPost_add").val()=="" || $("#sellerPost_add").val()==null){
		alert("职位不能为空");
		return;
	}
	if($("#sellerNumber_add").val()=="" || $("#sellerNumber_add").val()==null){
		alert("工号不能为空");
		return;
	}
	$("#addSellerForm").submit();
}

function showUpdateSellerDiv(pageNow, index){
	$("#backGroud").show();
	$("#updateDiv").show();
	
	if($("#companySelect_add").html()==""){
		readyAllCompany("companySelect_u","marketSelect_u");
	}
	$("#companySelect_u").val(sellers[index].superMarket.company.id);
	if($("#marketSelect_u").html()==""){
		readyAllMarket("companySelect_u","marketSelect_u");
	}
	$("#marketSelect_u").val(sellers[index].superMarket.id);
	
	$("#sellerId_u").val(sellers[index].id);
	$("#sellerName_u").val(sellers[index].name);
	$("#sellerPost_u").val(sellers[index].post);
	$("#sellerNumber_u").val(sellers[index].jobNumber);
	$("#sellerYibiao_u").val(sellers[index].yibiao);
	$("#sellerKaoqin_u").val(sellers[index].kaoqin);
	$("#sellerZhishi_u").val(sellers[index].zhishi);
	$("#sellerJiqiao_u").val(sellers[index].jiqiao);
	
	$("#updateSellerButton").bind("click", function(){updateSeller(pageNow);});
}

function closeUpdateSellerDiv(){
	$("#updateSellerForm")[0].reset();
	$("#updateSellerButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function updateSeller(pageNow){
	if($("#sellerName_u").val()=="" || $("#sellerName_u").val()==null){
		alert("促销员姓名不能为空");
		return;
	}
	if($("#marketSelect_u").val()=="" || $("#marketSelect_u").val()==null){
		alert("店名不能为空");
		return;
	}
	if($("#sellerPost_u").val()=="" || $("#sellerPost_u").val()==null){
		alert("职位不能为空");
		return;
	}
	if($("#sellerNumber_u").val()=="" || $("#sellerNumber_u").val()==null){
		alert("工号不能为空");
		return;
	}
	
	var tmp2=root+"/admin/seller!updateSeller";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"seller.id":$("#sellerId_u").val(),"seller.name":$("#sellerName_u").val(),"seller.jobNumber":$("#sellerNumber_u").val(),
			"seller.yibiao":$("#sellerYibiao_u").val(),"seller.kaoqin":$("#sellerKaoqin_u").val(),"seller.zhishi":$("#sellerZhishi_u").val(),
			"seller.jiqiao":$("#sellerJiqiao_u").val(),"seller.post":$("#sellerPost_u").val(),"superMarket.id": $("#marketSelect_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdateSellerDiv();
			if(json.code=="y"){
				findSellerList(pageNow);
			}else{
				alert("修改失败，请稍后重试！");
			}
		}
	});
}

function deleteSeller(sellerId){
	var ids = "";
	if(sellerId==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个商品！");
			return;
		}
	}
	var tmp2=root+"/admin/seller!deleteSeller";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":curPageNum, "ids":ids, "isDesc": isDesc, "superMarket.id":$("#marketSelect").val(), "seller.name": $("#sellerName").val(),
				"seller.jobNumber": $("#sellerNumber").val(), "seller.post": $("#sellerPost").val(), "seller.id": sellerId},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findSellerList(json.curPageNum);
		}
	});
}